package com.sf.dlb.web.controller;

import com.sf.dlb.bean.UserWarningRquestBean;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.web.service.UserWarningService;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by tanjing on 2019/4/22.
 */
@RestController
@RequestMapping(value="/v1/user/")
public class UserController  extends BaseController{
    private static Logger logger = LoggerFactory.getLogger(SysUserController.class);
    @Autowired
    private UserWarningService userWarningService;
    /**
     * 用户预警列表
     */
    @GetMapping(value = "/warningList/{page}/{size}")
    @ResponseBody
    public Object warningList(HttpServletRequest request, @PathVariable("page") int page, @PathVariable("size")int size) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        if(page==0){
            page=1;//mybtis默认 从1开始算
        }
        if(size==0){
            size=20;//默认20条
        }
        resultVO=userWarningService.warningList(page,size);
        logger.info("warningList response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 用户预警列表详情
     */
    @GetMapping(value = "/warningListDetails/{userId}")
    @ResponseBody
    public Object warningListDetails(HttpServletRequest request, @PathVariable("userId") String userId) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        if(StringUtils.isEmpty(userId)){
            return ResultVOUtil.error(ResponseConstant.ERROR, "用户id不能为空");
        }
        resultVO=userWarningService.warningListDetails(userId);
        logger.info("warningListDetails response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 用户预警处理
     */
    @PostMapping(value = "/warningHandler")
    @ResponseBody
    public Object warningHandler(HttpServletRequest request, @RequestBody UserWarningRquestBean userWarningRquestBean) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        if(null==userWarningRquestBean){
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        if(StringUtils.isEmpty(userWarningRquestBean.getUserId())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "用户id不能为空");
        }
        if(StringUtils.isEmpty(userWarningRquestBean.getDealtType())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "处理类型不能为空");
        }
        resultVO=userWarningService.warningHandler(userWarningRquestBean.getUserId(),userWarningRquestBean.getDealtType(),userWarningRquestBean.getDlbNumber());
        logger.info("warningHandler response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
}
